package com.cn.nullframe.controllor;

import com.cn.nullframe.mapper.UserMapper;
import com.cn.nullframe.pojo.User;
import com.cn.nullframe.service.UserService;
import com.cn.nullframe.service.base.CommonService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * restful接口测试
 * url:http://localhost:8080/nullframe/rest/user
 */
@RequestMapping("user")
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CommonService commonService;


    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 测试queryOne
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "queryOne", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public User queryOne(@RequestBody String json, ServletRequest request) {
        try {
            Map<String, Object> map = MAPPER.readValue(json, Map.class);
            User user = new User();
            user.setId((long) (int) map.get("id"));
            User userResult = this.userService.queryOne(user);
            return userResult;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 测试queryListByWhere
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "queryListByWhere", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public List<User> queryListByWhere(@RequestBody String json, ServletRequest request) {
        try {
            Map<String, Object> map = MAPPER.readValue(json, Map.class);
            User user = new User();
            user.setName((String) map.get("name"));
            List<User> users = this.userService.queryListByWhere(user);
            return users;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 测试queryListByWhere
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "udpate", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public void udpate(@RequestBody String json, ServletRequest request) {
        try {
            Map<String, Object> map = MAPPER.readValue(json, Map.class);
            User user = new User();
            user.setId((long) (int) map.get("id"));
            user.setName((String) map.get("name"));
//            this.userService.udpate(user);
            this.userService.updateSelective(user);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试queryListByWhere
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "deleteById", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public void deleteById(@RequestBody String json, ServletRequest request) {
        try {
            Map<String, Object> map = MAPPER.readValue(json, Map.class);
            this.userService.deleteById((long) (int) map.get("id"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试分页插件PageHelper
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "testPageHelper", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public PageInfo<User> testPageHelper(@RequestBody String json, ServletRequest request) {
        Map<String, Object> map = null;
        try {
            map = MAPPER.readValue(json, Map.class);
            PageInfo<User> userPageInfo = this.userService.queryPageList((String) map.get("name"), (int) map.get("page"), (int) map.get("size"));
            return userPageInfo;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 测试事务是否生效Ttransaction
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "testTtransaction", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public void testTtransaction(@RequestBody String json, ServletRequest request) {
        this.userService.testUser();
    }

    /**
     * 一个参数的数据请求
     * http://localhost:8081/nullframe/rest/user/1
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "{userId}", method = RequestMethod.GET)
    public ResponseEntity<User> queryOne(@PathVariable("userId") Long id) {
        try {
            User user = new User();
            user.setId(id);
            User userResult = this.userService.queryOne(user);
            return ResponseEntity.ok(userResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

}
